/*
 * @lc app=leetcode.cn id=860 lang=golang
 *
 * [860] 柠檬水找零
 */

// @lc code=start
func lemonadeChange(bills []int) bool {
	wallet := make(map[int]int)
	wallet[5] = 0
	wallet[10] = 0

	for i := 0; i < len(bills); i++ {
		if bills[i] == 5 {
			wallet[5] = wallet[5] + 1
		}

		if bills[i] == 10 {
			if wallet[5] > 0 {
				wallet[5] = wallet[5] - 1
				wallet[10] = wallet[10] + 1
			} else {
				return false
			}
		}

		if bills[i] == 20 {
			if wallet[10] > 0 && wallet[5] > 0 {
				wallet[10] = wallet[10] - 1
				wallet[5] = wallet[5] - 1
			} else if wallet[5] >= 3 {
				wallet[5] = wallet[5] - 3
			} else {
				return false
			}
		}
	}
	return true
}

// @lc code=end

